/*
  ==============================================================================

  This is an automatically generated file created by the Jucer!

  Creation date:  4 Jun 2008 1:17:06 pm

  Be careful when adding custom code to these files, as only the code within
  the "//[xyz]" and "//[/xyz]" sections will be retained when the file is loaded
  and re-saved.

  Jucer version: 1.11

  ------------------------------------------------------------------------------

  The Jucer is part of the JUCE library - "Jules' Utility Class Extensions"
  Copyright 2004-6 by Raw Material Software ltd.

  ==============================================================================
*/

//[Headers] You can add your own extra header files here...
#include "KetonalFormEditorWindow.h"
//[/Headers]

#include "KetonalFormEditor.h"


//[MiscUserDefs] You can add your own user definitions and misc code here...
//[/MiscUserDefs]

//==============================================================================
KetonalFormEditor::KetonalFormEditor (KetonalConfig *_c, KetonalFormEditorWindow *_p)
    : addFormElement (0)
{
    addAndMakeVisible (addFormElement = new ImageButton (T("Add Form Element")));
    addFormElement->setTooltip (T("Add Form Element"));
    addFormElement->addButtonListener (this);

    addFormElement->setImages (false, true, true,
                               ImageCache::getFromMemory (listadd_png, listadd_pngSize), 0.5723f, Colour (0x0),
                               ImageCache::getFromMemory (listadd_png, listadd_pngSize), 1.0000f, Colour (0x0),
                               ImageCache::getFromMemory (listadd_png, listadd_pngSize), 1.0000f, Colour (0x5bfffba5));

    //[UserPreSize]
	parent = _p;
	formElement *f = new formElement(this, 0);
	f->setBounds (16, 16, 220, f->getHeight());
	formElements.add (f);
	addAndMakeVisible (f);
    //[/UserPreSize]

    setSize (256, 156);

    //[Constructor] You can add your own custom stuff here..
	config				= _c;
	XmlElement *form	= 0;
	nElements = 1;

	form = config->getFormData();

	if (form)
	{
		int n = form->getNumChildElements();

		for (int x=1; x<n; x++)
		{
			addElement();
		}

		int k=0;
		forEachXmlChildElement (*form, e)
		{
			if (e->hasTagName (T("HTMLformElement")))
			{
				if (formElements[k])
				{
					formElements[k]->setXmlData(e);
				}
				k++;
			}
		}
	}
	else
		nElements			= 1;

	Logger::writeToLog (String::formatted (T("formEditor has %d elements"), nElements));
    //[/Constructor]
}

KetonalFormEditor::~KetonalFormEditor()
{
    //[Destructor_pre]. You can add your own custom destruction code here..
	XmlElement *form = new XmlElement (T("HTMLForm"));

	for (int x=0; x<formElements.size(); x++)
	{
		if (formElements[x])
		{
			form->addChildElement (formElements[x]->getXmlElement());
		}
	}

	config->setFormData (form);

    //[/Destructor_pre]

    deleteAndZero (addFormElement);

    //[Destructor]. You can add your own custom destruction code here..
    //[/Destructor]
}

//==============================================================================
void KetonalFormEditor::paint (Graphics& g)
{
    //[UserPrePaint] Add your own custom painting code here..
    //[/UserPrePaint]

    GradientBrush gradient_1 (Colour (0xffcecece),
                              (float) (proportionOfWidth (0.5000f)), (float) (proportionOfHeight (0.5000f)),
                              Colour (0xffa1a1a1),
                              (float) (getWidth() - 19), 16.0f,
                              true);
    g.setBrush (&gradient_1);
    g.fillRoundedRectangle (0.0f, 0.0f, 256.0f, (float) (proportionOfHeight (0.9800f)), 10.5000f);

    //[UserPaint] Add your own custom painting code here..
    //[/UserPaint]
}

void KetonalFormEditor::resized()
{
    addFormElement->setBounds (getWidth() - 27, getHeight() - 16 - 16, 16, 16);
    //[UserResized] Add your own custom resize handling here..
    //[/UserResized]
}

void KetonalFormEditor::buttonClicked (Button* buttonThatWasClicked)
{
    //[UserbuttonClicked_Pre]
    //[/UserbuttonClicked_Pre]

    if (buttonThatWasClicked == addFormElement)
    {
        //[UserButtonCode_addFormElement] -- add your button handler code here..
		addElement();
        //[/UserButtonCode_addFormElement]
    }

    //[UserbuttonClicked_Post]
    //[/UserbuttonClicked_Post]
}



//[MiscUserCode] You can add your own definitions of your custom methods or any other code here...
void KetonalFormEditor::mouseDrag (const MouseEvent& e)
{
	myDragger.dragComponent (getParentComponent(), e);
}

void KetonalFormEditor::mouseDown (const MouseEvent& e)
{
	myDragger.startDraggingComponent (getParentComponent(), 0);
}

void KetonalFormEditor::addElement ()
{
	int oldHeight = getHeight();
	formElement *f = new formElement(this, nElements);
	f->setBounds (16, 16 + (nElements * f->getHeight()), 220, f->getHeight());
	formElements.add (f);
	addAndMakeVisible (f);
	
	nElements++;

	setSize (256, oldHeight + (f->getHeight()));
	parent->setElements (nElements);
}

void KetonalFormEditor::removeElement (int i)
{
	Logger::writeToLog (String::formatted (T("FormEditorComponent: remove element %d"), i));

	if (nElements == 1)
		return;

	int oldHeight = getHeight();

	if (formElements[i])
	{
		int elmHeight = formElements[i]->getHeight();

		setSize (256, oldHeight - elmHeight);

		formElements[i]->setRemove();
	}

	nElements--;

	repositionElements();

	((KetonalFormEditorWindow *)getParentComponent())->setElements (nElements);
}

void KetonalFormEditor::repositionElements()
{
	Logger::writeToLog (String::formatted (T("FormEditorComponent: formElements size: %d elements: %d"),formElements.size(), nElements));

	for (int x=0; x<formElements.size(); x++)
	{
		if (formElements[x])
		{
			if (!formElements[x]->getRemove())
			{
				formElements[x]->setBounds (16, 16 + (x * formElements[x]->getHeight()), 220, formElements[x]->getHeight());
				formElements[x]->setIndex (x);
			}
			else
			{
				Logger::writeToLog (String::formatted (T("FormEditorComponent: delete element %d"), x));
				formElements.remove (x,true);
				x--;
			}
		}
	}

	resized();
}
//[/MiscUserCode]


//==============================================================================
#if 0
/*  -- Jucer information section --

    This is where the Jucer puts all of its metadata, so don't change anything in here!

BEGIN_JUCER_METADATA

<JUCER_COMPONENT documentType="Component" className="KetonalFormEditor" componentName=""
                 parentClasses="public Component" constructorParams="KetonalConfig *_c, KetonalFormEditorWindow *_p"
                 variableInitialisers="" snapPixels="8" snapActive="1" snapShown="1"
                 overlayOpacity="0.330000013" fixedSize="0" initialWidth="256"
                 initialHeight="156">
  <BACKGROUND backgroundColour="ffffff">
    <ROUNDRECT pos="0 0 256 98%" cornerSize="10.5" fill=" radial: 50% 50%, 19R 16, 0=ffcecece, 1=ffa1a1a1"
               hasStroke="0"/>
  </BACKGROUND>
  <IMAGEBUTTON name="Add Form Element" id="db190bfa727ee814" memberName="addFormElement"
               virtualName="" explicitFocusOrder="0" pos="27R 16Rr 16 16" tooltip="Add Form Element"
               buttonText="Add Form Element" connectedEdges="0" needsCallback="1"
               radioGroupId="0" keepProportions="1" resourceNormal="listadd_png"
               opacityNormal="0.572327018" colourNormal="0" resourceOver="listadd_png"
               opacityOver="1" colourOver="0" resourceDown="listadd_png" opacityDown="1"
               colourDown="5bfffba5"/>
</JUCER_COMPONENT>

END_JUCER_METADATA
*/
#endif

//==============================================================================
// Binary resources - be careful not to edit any of these sections!

// JUCER_RESOURCE: listadd_png, 446, "C:\list-add.png"
static const unsigned char resource_KetonalFormEditor_listadd_png[] = { 137,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,0,16,0,0,0,16,8,6,0,0,0,31,243,255,97,0,0,0,1,115,82,71,66,0,174,206,28,233,0,
0,0,6,98,75,71,68,0,255,0,255,0,255,160,189,167,147,0,0,0,9,112,72,89,115,0,0,11,19,0,0,11,19,1,0,154,156,24,0,0,0,7,116,73,77,69,7,216,6,2,9,6,13,48,202,233,125,0,0,1,62,73,68,65,84,56,203,181,147,49,
106,2,81,16,134,127,23,9,242,144,136,108,132,96,25,236,4,193,82,175,161,173,55,240,14,158,195,218,54,150,94,194,34,73,97,161,32,15,197,102,147,102,119,89,217,89,221,125,51,41,76,214,128,174,137,9,153,
110,152,153,239,253,255,48,15,248,99,228,178,10,106,164,58,0,172,143,148,195,94,56,190,10,48,212,67,105,220,55,176,147,29,102,175,51,244,31,250,103,123,243,89,128,128,3,232,189,6,9,193,51,94,166,133,76,
0,9,193,101,23,196,4,18,250,5,128,9,48,7,80,196,209,229,37,170,145,234,12,90,131,199,45,111,145,72,130,4,9,42,183,21,56,57,7,36,132,50,151,177,241,54,240,141,143,72,34,148,172,18,38,47,147,110,216,11,
199,159,10,114,213,187,42,214,241,26,44,12,195,6,43,89,193,75,60,232,88,195,73,28,4,249,32,213,219,44,52,211,199,83,11,62,251,112,141,11,146,131,231,229,126,137,249,110,14,6,159,200,54,98,78,118,32,250,
77,35,224,0,177,196,8,57,4,10,72,135,235,86,29,102,123,28,82,145,2,0,185,120,97,173,167,150,216,11,91,236,133,45,237,231,118,102,179,149,85,48,48,103,37,255,24,192,114,244,126,110,15,223,222,65,209,42,
162,118,83,59,120,38,117,61,96,58,155,118,191,126,38,252,87,188,3,21,118,166,154,187,253,123,139,0,0,0,0,73,69,78,68,174,66,96,130,0,0};

const char* KetonalFormEditor::listadd_png = (const char*) resource_KetonalFormEditor_listadd_png;
const int KetonalFormEditor::listadd_pngSize = 446;

// JUCER_RESOURCE: listremove_png, 260, "C:\list-remove.png"
static const unsigned char resource_KetonalFormEditor_listremove_png[] = { 137,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,0,16,0,0,0,16,8,6,0,0,0,31,243,255,97,0,0,0,1,115,82,71,66,0,174,206,28,233,
0,0,0,6,98,75,71,68,0,255,0,255,0,255,160,189,167,147,0,0,0,9,112,72,89,115,0,0,11,19,0,0,11,19,1,0,154,156,24,0,0,0,7,116,73,77,69,7,216,6,2,9,6,10,174,174,124,222,0,0,0,132,73,68,65,84,56,203,99,96,
24,5,3,15,24,25,24,24,24,246,249,249,53,11,105,107,171,147,162,241,237,213,171,183,156,55,109,170,101,97,96,96,96,144,84,80,168,17,103,99,35,201,102,118,5,5,6,6,6,134,133,44,12,12,12,12,95,183,108,97,
120,119,255,62,73,6,124,85,84,100,96,96,96,224,98,97,96,96,96,184,251,253,123,239,95,37,37,235,255,80,47,17,3,158,126,252,248,152,129,129,225,19,35,82,88,240,49,48,48,48,145,224,136,127,12,12,12,159,70,
147,17,21,0,0,82,24,28,171,91,208,167,196,0,0,0,0,73,69,78,68,174,66,96,130,0,0};

const char* KetonalFormEditor::listremove_png = (const char*) resource_KetonalFormEditor_listremove_png;
const int KetonalFormEditor::listremove_pngSize = 260;
